<?php

/**
 * Otpusk form.
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class OtpuskForm extends BaseOtpuskForm
{
  public function configure()
  {
  	unset( $this['pub']);
  	
  	    $this->setWidgets(array(
      'id'          => new sfWidgetFormInputHidden(),
      'user_id'     => new sfWidgetFormInput( array (), array ('value' => sfContext::getInstance()->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ))),
  	  'date_start' => new sfWidgetFormI18nDate(array('culture' =>sfContext::getInstance()->getUser()->getCulture())),
  	  'date_expire' => new sfWidgetFormI18nDate(array('culture' =>sfContext::getInstance()->getUser()->getCulture())),

      
    ));
  	
    $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(array('callback' => array($this, 'checkDate')))
    );
    
    $this->validatorSchema['date_start'] = new sfValidatorDate();
    $this->validatorSchema['date_expire'] = new sfValidatorDate();
    
    $this->validatorSchema->setOption ( 'allow_extra_fields', true );
    $this->widgetSchema->setNameFormat('otpusk[%s]');	
    
  }
  
  public function checkDate($validator, $values)
  {
  	$now = new sfDate($values['date_start']);
    if ($values['date_expire'] > $now->addDay(90)->dump())
    {
      // password is not correct, throw an error
      throw new sfValidatorError($validator, 'Отпуск может быть максимум на 90 дней');
    }
   $now = new sfDate($values['date_start']);
   $n = new sfDate();
      if ($values['date_expire'] < $now->dump())
    {
    	//echo $now->dump();
    	//echo $values['date_expire'];
    	//die;
      throw new sfValidatorError($validator, 'Окончание отпуска не может быть раньше даты начала');
    }
    
     if ($values['date_start'] < $n->dump())
    {

      throw new sfValidatorError($validator, 'Начала отпуска не может быть раньше сегоднешней даты');
    }
    
    
    // password is correct, return the clean values
    return $values;
  }
  
  
}
